package org.poker.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;

import org.poker.Poker;
import org.poker.exception.PokerException;

public class Frame extends Actions 
{
	private static final long serialVersionUID = 8014491457292675115L;

	private Container containerPrincipal;
	private Container containerSuperior;
	private Container containerInferior;
	private JButton[] buttonsSuperior;
	private JButton[] buttonsInferior;
	private JLabel[] labelsInferior;
	
	public Frame() {
		
		super("Video Poker");
		
		// CONTAINER SUPERIOR
		this.containerSuperior = new Container();
		this.containerSuperior.setLayout(new GridLayout(0,5));
	
		try {
			Poker.newGame();
		} catch (PokerException e1) {
			e1.printStackTrace();
		}
		
		// buttons superiores
		this.buttonsSuperior = new JButton[5];
		for(int i=0; i<this.buttonsSuperior.length; i++) {   
			try {
				this.buttonsSuperior[i] = new JButton(Poker.getHandCard(i));
				this.buttonsSuperior[i].setBackground(Color.white);
				this.buttonsSuperior[i].setPreferredSize(new Dimension(100, 300));
				this.buttonsSuperior[i].setFont(new Font(null, Font.BOLD, 60));
				this.buttonsSuperior[i].setForeground(Poker.isRedCardInHand(i)?Color.RED:Color.BLACK);
				this.buttonsSuperior[i].setName("button_"+i);
			} catch (PokerException e) {
				e.printStackTrace();
			}
			this.buttonsSuperior[i].addActionListener(super.actionMarcarDesmarcarCarta);
			this.buttonsSuperior[i].addActionListener(super.eventControl);
			this.buttonsSuperior[i].addKeyListener(super.keyControl);
			
	
			ImageIcon image = new ImageIcon("img//carta_back3.JPG");
			//this.buttonsSuperior[i].setIcon(image);
			this.buttonsSuperior[i].setDisabledIcon(image);
			
			this.containerSuperior.add(this.buttonsSuperior[i]);
		}
		
		super.actionDistribuirNovasCartas.putValue("frame", this);
		
		// CONTAINER INFERIOR
		this.containerInferior = new Container();
		this.containerInferior.setLayout(new GridLayout(2, 2));
		this.containerInferior.setPreferredSize(new Dimension(100, 120));
		
		// labels inferiores
		this.labelsInferior = new JLabel[2];
		this.labelsInferior[0] = new JLabel(Poker.printPoints(),JLabel.LEFT);
		this.labelsInferior[0].setFont(new Font(null, Font.BOLD, 20));
		this.labelsInferior[1] = new JLabel("",JLabel.LEFT);
		this.labelsInferior[1].setFont(new Font(null, Font.BOLD, 25));
		for(JLabel label : this.labelsInferior) 
			this.containerInferior.add(label);
		
		super.actionTrocarCartasMarcadas.putValue("labelsInferior", this.labelsInferior);
		super.actionDistribuirNovasCartas.putValue("labelsInferior", this.labelsInferior);
		
		// buttons inferiores
		String[] labelButtonsInferior = new String[] {"Trocar","Novas"};
		this.buttonsInferior = new JButton[labelButtonsInferior.length];
		for(int i=0; i<buttonsInferior.length; i++) {
			this.buttonsInferior[i] = new JButton(labelButtonsInferior[i]);
			this.buttonsInferior[i].setFont(new Font(null, Font.BOLD, 30));
			this.buttonsInferior[i].addActionListener(super.eventControl);
			this.buttonsInferior[i].addKeyListener(super.keyControl);
			this.containerInferior.add(this.buttonsInferior[i]);
		}
		this.buttonsInferior[1].setEnabled(false);
		this.buttonsInferior[0].addActionListener(actionTrocarCartasMarcadas);
		this.buttonsInferior[1].addActionListener(actionDistribuirNovasCartas);
		
		super.actionTrocarCartasMarcadas.putValue("buttonsSuperior", this.buttonsSuperior);
		super.actionMarcarDesmarcarCarta.putValue("buttonsSuperior", this.buttonsSuperior);
		super.actionDistribuirNovasCartas.putValue("buttonsSuperior", this.buttonsSuperior);
		super.actionTrocarCartasMarcadas.putValue("buttonsInferior", this.buttonsInferior);
		super.actionMarcarDesmarcarCarta.putValue("buttonsInferior", this.buttonsInferior);
		super.actionDistribuirNovasCartas.putValue("buttonsInferior", this.buttonsInferior);
		
		
		// CONTAINER PRINCIPAL
		this.containerPrincipal = new Container();	
		this.containerPrincipal.setLayout(new BorderLayout());
		this.containerPrincipal.add(BorderLayout.NORTH, containerSuperior);
		this.containerPrincipal.add(BorderLayout.SOUTH, containerInferior);
		super.setContentPane(this.containerPrincipal);
	}
}
